/**  
 * @Title: HttpAccepterTest.java
 * @Package com.icss.trans.accept.httppoll
 * @Description:
 * @author ICSS
 * @date 2015-3-25 下午4:52:24
 * @version V1.0  
 */ 
package com.icss.trans.accept.httppoll;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;


/**
 * @ClassName: HttpAccepterTest
 * @Description: 
 * @author ICSS
 * @date 2015-3-25 下午4:52:24
 *
 */
public class HttpAccepterTest {

    @Test
    public void testAccept() {
        AcceptParam acceptParam = new AcceptParam();
        acceptParam.setAppCode("ICSS_ETL");
        acceptParam.setDataTransType("test001");
        acceptParam.setKey("key001");
        try {
            HttpAccepter.accept(acceptParam, new IAnalyzeFile() {
                @Override
                public void execute(String fileName, InputStream fileInputStream) {
                    System.out.println("ssss----" + fileName);
                    try {
                        FileUtils.writeByteArrayToFile(new File("E:/test/temp/" + fileName), IOUtils.toByteArray(fileInputStream));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testAcceptBuilder() {
        AcceptParam acceptParam = new AcceptParam.Builder().appCode("ICSS_ETL")
                .dataTransType("test001")
                .key("key001")
                .build();
        try {
            HttpAccepter.accept(acceptParam, new IAnalyzeFile() {
                @Override
                public void execute(String fileName, InputStream fileInputStream) {
                    // 测试将文件流写入到本地
                    try {
                        FileUtils.writeByteArrayToFile(new File("E:/test/temp/" + fileName), 
                                IOUtils.toByteArray(fileInputStream));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testScheduledAccept() {
        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);
        schedule.scheduleWithFixedDelay(new Runnable() {
            
            @Override
            public void run() {
                // 每隔2分钟进行一次http poll任务处理
            }
        }, 1, 2, TimeUnit.MINUTES);
    }
}
